package homeword;

import java.util.Random;
import java.util.Scanner;

public class 双色球 {
    // 常量定义
    public static final int RED_BALL_COUNT = 6;
    public static final int BLUE_BALL_MIN = 1;
    public static final int BLUE_BALL_MAX = 16;
    public static final int RED_BALL_MIN = 1;
    public static final int RED_BALL_MAX = 33;

    public static void main(String[] args) {
        // 生成中奖号码
        int[] arr = new int[RED_BALL_COUNT + 1];
        Random random = new Random();
        for (int i = 0; i < RED_BALL_COUNT; ) {
            int redNumber = random.nextInt(RED_BALL_MAX) + 1;
            if (!isDuplicate(arr, redNumber, i)) {
                arr[i] = redNumber;
                i++;
            }
        }
        // 篮球的中奖号码
        int blueNumber = random.nextInt(BLUE_BALL_MAX) + 1;
        arr[RED_BALL_COUNT] = blueNumber;
        System.out.print("本期中奖号码：");
        printArr(arr);

        // 用户输入号码
        int[] userInputArr = userInput();
        int redCount = 0;
        int blueCount = 0;
        // 计算红球中奖个数
        for (int i = 0; i < RED_BALL_COUNT; i++) {
            for (int j = 0; j < RED_BALL_COUNT; j++) {
                if (arr[i] == userInputArr[j]) {
                    redCount++;
                    break;
                }
            }
        }
        System.out.println("恭喜你中奖的红球有" + redCount + "个");
        // 计算篮球是否中奖
        if (arr[RED_BALL_COUNT] == userInputArr[RED_BALL_COUNT]) {
            blueCount = 1;
            System.out.println("恭喜你中奖的篮球有1个");
        } else {
            System.out.println("你的篮球没有中奖哦，再接再厉！");
        }
        // 判断获得几等奖
        if (sixthPrize(redCount, blueCount)) {
            System.out.println("恭喜你中了六等奖获得5元奖金");
        } else if (fifthPrize(redCount, blueCount)) {
            System.out.println("恭喜你中了五等奖获得10元奖金");
        } else if (fourthPrize(redCount, blueCount)) {
            System.out.println("恭喜你中了四等奖获得200元奖金");
        } else if (thirdPrize(redCount, blueCount)) {
            System.out.println("恭喜你中了三等奖获得3000元奖金");
        } else if (secondPrize(redCount, blueCount)) {
            System.out.println("恭喜你中了二等奖获得500万元奖金");
        } else if (firstPrize(redCount, blueCount)) {
            System.out.println("恭喜你中了一等奖获得100万元奖金");
        } else {
            System.out.println("很遗憾，未中奖。");
        }

        System.out.print("你输入的号码是：");
        printArr(userInputArr);
        System.out.print("中奖号码是：");
        printArr(arr);
    }

    // 用户输入号码，包含红球查重
    public static int[] userInput() {
        Scanner scanner = new Scanner(System.in);
        int[] userArr = new int[RED_BALL_COUNT + 1];
        for (int i = 0; i < RED_BALL_COUNT; ) {
            System.out.print("请输入你的第" + (i + 1) + "个红球号码(1~33)：");
            int num = scanner.nextInt();
            if (num < RED_BALL_MIN || num > RED_BALL_MAX) {
                System.out.println("红球号码超出范围，请重新输入。");
                continue;
            }
            if (isDuplicate(userArr, num, i)) {
                System.out.println("红球号码重复，请重新输入。");
                continue;
            }
            userArr[i] = num;
            i++;
        }
        while (true) {
            System.out.print("请输入你的蓝球号码(1~16)：");
            int blue = scanner.nextInt();
            if (blue >= BLUE_BALL_MIN && blue <= BLUE_BALL_MAX) {
                userArr[RED_BALL_COUNT] = blue;
                break;
            } else {
                System.out.println("蓝球号码超出范围，请重新输入。");
            }
        }
        return userArr;
    }

    // 判断数组前length个元素中是否有重复
    public static boolean isDuplicate(int[] arr, int number, int length) {
        for (int i = 0; i < length; i++) {
            if (arr[i] == number) {
                return true;
            }
        }
        return false;
    }

    public static void printArr(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i] + " ");
        }
        System.out.println(); // 打印完所有数字后换行
    }

    // 奖项判断方法命名规范
    public static boolean sixthPrize(int red, int blue) {
        return (red == 0 && blue == 1) || (red == 1 && blue == 1);
    }

    public static boolean fifthPrize(int red, int blue) {
        return (red == 2 && blue == 1) || (red == 3 && blue == 1) || (red == 4 && blue == 0);
    }

    public static boolean fourthPrize(int red, int blue) {
        return (red == 4 && blue == 1) || (red == 5 && blue == 0);
    }

    public static boolean thirdPrize(int red, int blue) {
        return (red == 5 && blue == 1);
    }

    public static boolean secondPrize(int red, int blue) {
        return (red == 6 && blue == 0);
    }

    public static boolean firstPrize(int red, int blue) {
        return (red == 6 && blue == 1);
    }

}
